﻿function Install-SharePointLangPack
{
    <# 
     .Synopsis
      Launches the SharePoint Language Pack Installer.

     .Description
      This script launches the SharePoint Language Pack Installer

     .Parameter PathToSPLangPackInstallFiles
      The full file system path where the installation source files for SharePoint are located. 
      The default value for this parameter is 'C:\ProgramData\SharePoint Total Automation Toolkit\Software\05-SP2013LangPacks'      

     .Example
       Install-SharePointLangPack

     .Example
       Install-SharePointLangPack -PathToSPLangPackInstallFiles 'C:\ProgramData\SharePoint Total Automation Toolkit\Software\05-SP2013LangPacks\es-es'       

    #>
    [CmdletBinding(DefaultParametersetName="CustomPaths")]
    param([parameter(ParameterSetName = "CustomPaths", 
                     Mandatory = $false,
                     HelpMessage = "The full file system path where the installation source files for SharePoint are located. The default value for this parameter is 'C:\ProgramData\SharePoint Total Automation Toolkit\Software\05-SP2013LangPacks'")]
                     [string]$PathToSPLangPackInstallFiles = ($env:ProgramData + "\SharePoint Total Automation Toolkit\Software\05-SP2013LangPacks"))


    switch ($PSCmdlet.ParameterSetName)
    {
        "CustomPaths"
        {
        }    
        default
        {
        }    
    }

	$rebootStatus = Get-PendingReboot
	if ($rebootStatus.RebootPending -eq $true)
	{
		Write-Host "NOTE: A system restart is required. Please restart the system prior to continuing with the language pack installation." -ForegroundColor Yellow
		Write-Host "      When the system restarts please rerun this script until you no longer receive a restart request." -ForegroundColor Yellow
		throw 
	}

    # Initialize Progress Variables
    [int]$progressBarId = 100
    $startTime = [System.DateTime]::Now

    [System.Xml.XmlDocument]$currentConfigXml = 
@"
<Configuration>
	<Setting Id="OSERVERLPK" Value="1"/>
	<Setting Id="USINGUIINSTALLMODE" Value="0"/>
	<Logging Type="verbose" Path="%temp%" Template="SharePoint Products Language Pack Setup(*).log"/>
	<Display Level="none" CompletionNotice="no" />
</Configuration>
"@

    # Save the newly generated config.xml file to the local temp directory
    $configXmlName = ("SpLangPackConfig_{0}.xml" -f (Get-Date -Format 'yyyy_MMM_dd_HH_mm_ss') )
    $configXmlPath = Join-Path $env:temp $configXmlName
    $currentConfigXml.Save($configXmlPath)


    #region Task 1

    # Launch the SharePoint Language Pack Installer
    $setupExe = ($PathToSPLangPackInstallFiles + "\setup.exe")
    $setupArgs = ("/config {0}" -f $configXmlPath)
    $setupExeInstance = [System.Diagnostics.Process]::Start($setupExe, $setupArgs)
    $step = 5
    $value = 0

    while (-not ($setupExeInstance.HasExited)){
        $currentRuntime = [System.DateTime]::Now - $startTime
        Write-Progress -Id $progressBarId -Activity "Installing SharePoint Language Packs" -PercentComplete ($value+=$step) -CurrentOperation "Executing the SharePoint Language Pack Installation Wizard" -Status ("The current operation has been running for {0}:{1}:{2}" -f $currentRuntime.Hours.ToString("00"), $currentRuntime.Minutes.ToString("00"), $currentRuntime.Seconds.ToString("00"))
     
        if ($value -ge 100) { $value = 0}

        Start-Sleep -Seconds 1
    }

    if ($setupExeInstance.ExitCode -eq 0)
    {
        Write-Host "...Success"
    }
    else
    {
		$exitCode = $setupExeInstance.ExitCode
        Write-Host
        Write-Error "          Installation of SharePoint Language Pack Failed with exit code ($exitCode)"
        throw
    }

    #endregion

    Write-Progress -Id $progressBarId -Activity "Installing SharePoint Language Packs" -Status "Finishing" -PercentComplete 100 -CurrentOperation "" -Completed
    
}